package org.projectwithoutname.ui.util;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleClientSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

/**
 * Open an OLE Windows Media Player and provide a way 
 * to execute a video inside the program.
 * @since 1.0
 */
public class MediaPlayerUtil {
	private static OleClientSite clientSite;
	private Display display = null;
	private OleFrame frame = null;
	private ScreenToolkit composite;
	
	public MediaPlayerUtil(ScreenToolkit layout) {
		this.display = layout.getDisplay();
		this.composite = layout;
	}
	
	/**
	 * initialize and open the media player
	 */
	public void open() {
//		shell.setText("Media Player Example");
		try {
			Point size = composite.getParent().getSize();
			frame = new OleFrame(composite, SWT.NONE);
			composite.putOnScreen(frame, SWT.FILL, SWT.FILL, size.x, size.y);
			clientSite = new OleClientSite(frame, SWT.NONE, "WMPlayer.OCX");
			clientSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
			composite.putOnScreen(clientSite, SWT.FILL, SWT.FILL, 0, 0);
			composite.pack();
//			addFileMenu();
		} catch (SWTError e) {
			System.out.println("Unable to open activeX control");
			display.dispose();
			return;
		}
	}

//	private void addFileMenu() {
//		final Shell shell = frame.getShell();
//		Menu menuBar = shell.getMenuBar();
//		if (menuBar == null) {
//			menuBar = new Menu(shell, SWT.BAR);
//			shell.setMenuBar(menuBar);
//		}
//		MenuItem fileMenu = new MenuItem(menuBar, SWT.CASCADE);
//		fileMenu.setText("&File");
//		Menu menuFile = new Menu(fileMenu);
//		fileMenu.setMenu(menuFile);
//		frame.setFileMenus(new MenuItem[] { fileMenu });
//
//		MenuItem menuFileOpen = new MenuItem(menuFile, SWT.CASCADE);
//		menuFileOpen.setText("Open...");
//		menuFileOpen.addSelectionListener(new SelectionAdapter() {
//			public void widgetSelected(SelectionEvent e) {
////				fileOpen();
//			}
//		});
//		MenuItem menuFileExit = new MenuItem(menuFile, SWT.CASCADE);
//		menuFileExit.setText("Exit");
//		menuFileExit.addSelectionListener(new SelectionAdapter() {
//			public void widgetSelected(SelectionEvent e) {
//				shell.dispose();
//			}
//		});
//	}
	
	/**
	 * open and execute a video inside the program.
	 *
	 * WARNING : Video's extensions that can be opened by this function 
	 * is limited by kind of extension supported by Windows Media Player(R), 
	 * wherefore it can be necessary the use of external codecs.  
	 */
	public void run(String filename) {
		if (filename != null) {
			OleAutomation player = new OleAutomation(clientSite);
			int playURL[] = player.getIDsOfNames(new String[] { "URL" });
			if (playURL != null) {
				Variant theFile = new Variant(filename);
				player.setProperty(playURL[0], theFile);
			}
			player.dispose();
		}
	}
}